<?


	//this is a specialisation of cms content fangler 
	//that is used for building pages
	include_once("cmsPageBuilderSiteConfig.php");
	include_once("cmsConfig.php");
	include_once("cmsPageBuilderConfig.php");

	
	class CmsPageBuilder extends CmsContentBuilder {
				
		function CmsPageBuilder() {
			parent::CmsContentBuilder();

			 $this->keys = array_merge_recursive(
			 	$this->keys,
			 	array(
			 		EXTRACT_STRING => array(
			 			"title",
			 			"topContent",
			 			"bottomContent",
			 			"relatedUrl1Text",
			 			"relatedUrl2Text",
			 			"relatedUrl3Text",
			 			"relatedUrl1Url",
			 			"relatedUrl2Url",
			 			"relatedUrl3Url",
			 			"bottomContentHeading",
			 			"pageDescription"
			 	)),
			 	array(
			 		EXTRACT_BOOLEAN => array( 
			 			"inlineMenu",
			 			"active"
			 		)
			 	),
			 	array (
			 		EXTRACT_NUMBER => array(
			 			"menuSection",
			 			"menuRank"
			 		)
			 	)
			 );
			 	
			 $this->requiredKeys = array_merge(
			 	array(
			 		"title",
			 		//"menuSection",
			 		"menuRank"
			 	),
			 	$this->requiredKeys
			 );
			 $this->dataModelKeyMapping = array_merge(
			 	$this->dataModelKeyMapping,
			 	array(
			 		"title" => "title",
			 		"inlineMenu" => "shortdescription",
			 		"topContent" => "content",
			 		"bottomContent" => "author",
			 		"menuSection" => "value",
			 		"menuRank" => "url",
			 		"relatedUrl1Text" => "organisationalname",
			 		"relatedUrl2Text" => "address",
			 		"relatedUrl3Text" => "fax",
			 		"relatedUrl1Url" => "authordepartment",
			 		"relatedUrl2Url" => "authorbio",
			 		"relatedUrl3Url" => "phone",
			 		"bottomContentHeading" => "email",
			 		"pageDescription" => "extra4",
			 		"active" => "active"
			 		
			 	)
			 );
			 
			 $this->formId = 187;//180;
			 $this->documentTypeCode = 'pagebuilder';//'docOrder';
			 $this->contentBlockClass = CMS_PAGEBUILDER_CONTENTBLOCK_CLASS;
			 
			$this->TINYMCE_CSS = CMS_PAGEBUILDER_TINYMCE_CSS;
			$this->TINYMCE_STYLES = CMS_PAGEBUILDER_TINYMCE_STYLES;
			$this->TINYMCE_TAGS = CMS_PAGEBUILDER_TINYMCE_TAGS;
			
			//$this->valueForKey('active') 
			$this->takeValueForKey(true,'active');
		}
		
		function validateMenuRank() {
			$menuRank = $this->valueForKey('menuRank');
			if (is_null($menuRank) || !is_numeric($menuRank) ) {
				$menuRank = 0;
			}
			$this->takeValueForKey($menuRank, 'menuRank');
			return true;
		}
		
		function generateForm(&$db){
			
			
			$self = $_SERVER['PHP_SELF'];
			$form = <<< EOD
<form action="$self" method="POST" enctype="multipart/form-data" >
EOD;
			
			// we need to check here is there has been an attempt to save...
			// looking a status message.
			
			if ( is_array($this->dict['invocationResults'])){
				$saveResult = extractBooleanWithKeyFromArray('save',$this->dict['invocationResults'],false);
			}

						
			if ($saveResult) {
				$message = "Your page \"". $this->valueForKey('title')."\" has been saved successfully.";						
				$okUrl = "/cms/html/index.php?fileNo=1&formid=187";
				ob_start();
				dialoguePage(
					array(
						DLG_MESSAGE => $message,
						DLG_RIGHT_LINK => "$okUrl",
						DLG_RIGHT_TEXT => "Ok"//,															
					)
				);
				$form .= ob_get_clean();				
			} else {// we are either showing a form.. or ..  um.. we are .. displaying errors?
	
				$id = $this->valueForKey('id');
				$titleFieldId = $this->keyForField('title');
				$title = str_replace('"','&quot;', $this->valueForKey('title'));
				
				$menuRank = $this->valueForKey('menuRank');
				$menuRankFieldId = $this->keyForField('menuRank');
				
				$menuSection = $this->valueForKey('menuSection');
				$menuSectionFieldId = $this->keyForField('menuSection');
				
				$invalidKeys = 	$this->valueForKey('invalidKeys');
				
				$form .= <<< EOD
		<div class="cms_browse_heading">
				<div class="cms_browse_heading_label">
					<h1>Page Builder</h1>
				</div>
			<div class="cms_browse_addrecord"></div>
	  	</div>					
			<div id="cms_browse_topdivider" class="cms_browse_divider"></div>					
			<table  border="0" cellspacing="0" cellpadding="0" class="cms_masteraddedit_table" >
	          <tr>
	            <td height="1" width="160"><img src="/cms/images/spacer.gif" height="1" width="160" /></td>
	            <td height="1"><img src="/cms/images/spacer.gif" height="1" width="1" /></td>
	          </tr>
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Page Id:</td>
		            <td class="cms_form_element" align="left">$id
		            </td>
		        </tr>	          
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Title:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$titleFieldId" name="$titleFieldId" value="$title" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('title')) {
					$form .= "<br /><span class=\"validationError\"> Title ".$invalidKeys['title'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
				</tr>
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label">Menu Section:</td>
					<td  class="cms_form_element" align="left">
EOD;

					$q = "select documentcode, title from content where documenttypecode='menusection' order by categorycode asc;";
					ob_start();
					optionList(array("id"=>$menuSectionFieldId, "class"=>"", "name"=>$menuSectionFieldId), true, $menuSection, 0,1,$q,$db);
					$form .= ob_get_clean();
				
				
				
				
				$form .= <<< EOD
					</td>
				</tr>
				
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label">Menu Rank:</td>
					<td  class="cms_form_element" align="left">
						<input type="text" id="$menuRankFieldId" name="$menuRankFieldId" value="$menuRank" class="cms_form_text" />
EOD;

				if (!$this->isKeyValid('menuRank')) {
					$form .= "<br /><span class=\"validationError\"> Menu Rank ".$invalidKeys['menuRank'].".</span>\n";
				}
/*				
				$form .= <<< EOD
					</td>
				</tr>
	      <tr>
		    <td colspan="2" class="cms_form_label">Landing Page Description:</td>
		  </tr>
		  <tr >
	            <td colspan="2" align="left" class="cms_form_element">
EOD;
			$pageDescriptionId = $this->keyForField('pageDescription');
			$pageDescription = $this->valueForKey('pageDescription');
		    $form .= tinyMceEditorWithIdAndContent($pageDescriptionId, $pageDescription, ($hack=array("inputClass" => "cmsMailout","tinyMceTags" => $this->TINYMCE_TAGS, "tinyMceCss" => $this->TINYMCE_CSS,"tinyMceStyles" => $this->TINYMCE_STYLES)));
			if (!$this->isKeyValid('pageDescription')) {
				$form .= "<br /><span class=\"validationError\"> Text Content ".$invalidKeys['pageDescription'].".</span>\n";
			}
			*/
				$form .= <<< EOD
		    </td>
	          </tr>
		<tr class="cms_masteraddedit_table"><td colspan="2">&nbsp;</td><tr>
	          
EOD;
	
				$topWebContentBlockId = $this->keyForField('topContent');
				$topWebContent = $this->valueForKey('topContent');
				
				$form .=<<< EOD
	      <tr>
		    <td colspan="2" class="cms_form_label">Top Content:</td>
		  </tr>
		  
		  
		  <tr >
	            <td colspan="2" align="left" class="cms_form_element">
EOD;
	
		    $form .= tinyMceEditorWithIdAndContent($topWebContentBlockId, $topWebContent, ($hack=array("inputClass" => "cmsMailout","tinyMceTags" => $this->TINYMCE_TAGS, "tinyMceCss" => $this->TINYMCE_CSS,"tinyMceStyles" => $this->TINYMCE_STYLES)));
	if (!$this->isKeyValid('bodyContent')) {
		$form .= "<br /><span class=\"validationError\"> Text Content ".$invalidKeys['topWebContent'].".</span>\n";
	}
				$form .= <<< EOD
		    </td>
	          </tr>			
EOD;
	
		$form .= <<< EOD
		<tr class="cms_masteraddedit_table"><td colspan="2">&nbsp;</td><tr>
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label">
						Show Inline Menu:
					</td>
					<td class="cms_form_element" align="left">
EOD;
					$inlineMenuId= $this->keyForField('inlineMenu');
					$inlineMenuChecked = ($this->valueForKey('inlineMenu')) ? true : false;
				
					$form .= jsDualStateCheckBox($inlineMenuId,'true','false', $inlineMenuChecked);
					$form .=<<< EOD
					</td>
				</tr> 
				<tr>
					<td colspan="2"><img src="Mailout/images/mailout_divider.gif"></td>
				</tr>
			</table>
			
			<table  border="0" cellspacing="0" cellpadding="0" class="cms_masteraddedit_table" >
	          <tr>
	            <td height="1" width="160"><img src="/cms/images/spacer.gif" height="1" width="160" /></td>
	            <td height="1"><img src="/cms/images/spacer.gif" height="1" width="1" /></td>
	          </tr>	
EOD;
				$cbNodeId=1;//check me
				//need to sort the contentblocks by their weight/order page position..
				$this->sortContentBlocks();//should this .. be done like this..
				foreach ($this->contentBlocks as $cb){
					$form .= <<< EOD
		                          <tr>
		                            <td colspan="2" align="right" valign="middle">
EOD;
					$form .= $cb->generateForm($db);
					$deleteContentBlockActionName = $this->keyForField('deleteContentBlock') . "_" . $cbNodeId;
					$deleteContentBlockActionArg = $deleteContentBlockActionName ."_1";// we also need to bolt a number here.. or something to tie it to the field
					$contentBlockIdToDelete = $cb->id();
					$form .= <<< EOD
		<input onClick="return imageSubmit(this);" type="image" src="Mailout/images/mailout_deleteContent.gif" id="$deleteContentBlockActionName" name="$deleteContentBlockActionName" value="Delete Content"/>
		<input type="hidden" id="$deleteContentBlockActionArg" name="$deleteContentBlockActionArg" value="$contentBlockIdToDelete"/>
									</td>
		                          </tr>
		                          
					  <tr>
					    <td colspan="2">
					      <img src="Mailout/images/mailout_divider.gif">
					    </td>
					  </tr>
EOD;
			
					$cbNodeId++;
				}
				
				//this is for bottom content here.. 
				// this will be able to be hidden???
				//$foot = $this->valueForKey('footer');
				//$form .= $foot->generateForm($db);
				
				$bottomWebContentBlockId = $this->keyForField('bottomContent');
				$bottomWebContent = $this->valueForKey('bottomContent');
				
				$bottomContentHeadingFieldId = $this->keyForField('bottomContentHeading');
				$bottomContentHeading = str_replace('"','&quot;', $this->valueForKey('bottomContentHeading'));
				
				
				$form .= "</table>";
	
				$addContentBlockAction = $this->keyForField('addContentBlock') . "_1";
				$saveMailoutAction = $this->keyForField('save') . "_1";
	
	
				$relatedUrl1TextFieldId = $this->keyForField('relatedUrl1Text');
				$relatedUrl1Text = str_replace('"','&quot;', $this->valueForKey('relatedUrl1Text'));

				$relatedUrl2TextFieldId = $this->keyForField('relatedUrl2Text');
				$relatedUrl2Text = str_replace('"','&quot;', $this->valueForKey('relatedUrl2Text'));

				$relatedUrl3TextFieldId = $this->keyForField('relatedUrl3Text');
				$relatedUrl3Text = str_replace('"','&quot;', $this->valueForKey('relatedUrl3Text'));

				$relatedUrl1UrlFieldId = $this->keyForField('relatedUrl1Url');
				$relatedUrl1Url = str_replace('"','&quot;', $this->valueForKey('relatedUrl1Url'));

				$relatedUrl2UrlFieldId = $this->keyForField('relatedUrl2Url');
				$relatedUrl2Url = str_replace('"','&quot;', $this->valueForKey('relatedUrl2Url'));

				$relatedUrl3UrlFieldId = $this->keyForField('relatedUrl3Url');
				$relatedUrl3Url = str_replace('"','&quot;', $this->valueForKey('relatedUrl3Url'));
				
				
				
	
				$form .= <<< EOD
				<div class="cmsMailoutMailActions">
				<input type="image" onClick="return imageSubmit(this);" src="Mailout/images/mailout_addContent.gif" id="$addContentBlockAction" name="$addContentBlockAction" value="Add Content"/>
				</div>
				<img src="/cms/html/Mailout/images/mailout_divider.gif" alt=""/>
				
				
			<table  border="0" cellspacing="0" cellpadding="0" class="cms_masteraddedit_table" >
	          <tr>
	            <td height="1" width="160"><img src="/cms/images/spacer.gif" height="1" width="160" /></td>
	            <td height="1"><img src="/cms/images/spacer.gif" height="1" width="1" /></td>
	          </tr>	
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label">Bottom Content Heading:</td>
					<td  class="cms_form_element" align="left">
						<input type="text" id="$bottomContentHeadingFieldId" name="$bottomContentHeadingFieldId" value="$bottomContentHeading" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('bottomContentHeading')) {
					$form .= "<br /><span class=\"validationError\"> Bottom Content Heading ".$invalidKeys['bottomContentHeading'].".</span>\n";
				}
				$form .= <<< EOD
					</td>
				</tr>	
	<tr >
		    <td colspan="2" class="cms_form_label">Bottom Content:</td>
		  </tr>
		  <tr >
	            <td colspan="2" align="left" class="cms_form_element">
EOD;
		$form .= tinyMceEditorWithIdAndContent($bottomWebContentBlockId, $bottomWebContent, ($hack=array("inputClass" => "cmsMailout", "tinyMceTags" => $this->TINYMCE_TAGS, "tinyMceCss" => $this->TINYMCE_CSS,"tinyMceStyles" => $this->TINYMCE_STYLES)));
	if (!$this->isKeyValid('bodyContent')) {
		$form .= "<br /><span class=\"validationError\"> Text Content ".$invalidKeys['bottomWebContent'].".</span>\n";
	}
				$form .= <<< EOD
		    </td>
	          </tr>	    
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" colspan="2">&nbsp;</td>
		        </tr>		          
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 1 Url:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl1UrlFieldId" name="$relatedUrl1UrlFieldId" value="$relatedUrl1Url" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl1Url')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 1 Url ".$invalidKeys['relatedUrl1Url'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>		          
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 1 Text:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl1TextFieldId" name="$relatedUrl1TextFieldId" value="$relatedUrl1Text" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl1Text')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 1 Text ".$invalidKeys['relatedUrl1Text'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>		          
<!-- -->
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 2 Url:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl2UrlFieldId" name="$relatedUrl2UrlFieldId" value="$relatedUrl2Url" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl2Url')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 2 Url ".$invalidKeys['relatedUrl2Url'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>		          
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 2 Text:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl2TextFieldId" name="$relatedUrl2TextFieldId" value="$relatedUrl2Text" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl2Text')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 2 Text ".$invalidKeys['relatedUrl2Text'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>			          
<!-- -->	                
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 3 Url:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl3UrlFieldId" name="$relatedUrl3UrlFieldId" value="$relatedUrl3Url" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl3Url')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 3 Url ".$invalidKeys['relatedUrl3Url'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>		          
				<tr class="cms_masteraddedit_table">
					<td class="cms_form_label" >Related Link 3 Text:</td>
		            <td class="cms_form_element" align="left">
						<input type="text" id="$relatedUrl3TextFieldId" name="$relatedUrl3TextFieldId" value="$relatedUrl3Text" class="cms_form_text" />
EOD;
				if (!$this->isKeyValid('relatedUrl3Text')) {
					$form .= "<br /><span class=\"validationError\"> Related Link 3 Text ".$invalidKeys['relatedUrl3Text'].".</span>\n";
				}
				$form .= <<< EOD
		            </td>
		        </tr>	
<!-- -->		        
					<td class="cms_form_label">
						Active:
					</td>
					<td class="cms_form_element" align="left">
EOD;
					$activeId = $this->keyForField('active');
					$activeChecked = ($this->valueForKey('active')) ? true : false;
				
					$form .= jsDualStateCheckBox($activeId,'true','false', $activeChecked);
					$form .=<<< EOD
					</td>
				</tr> 	          
	        </table>			
				<div class="cmsMailoutMailActions">
				<input type="image" onClick="return imageSubmit(this);" src="/cms/images/button_go.gif" id="$saveMailoutAction" name="$saveMailoutAction" value="Save Content"/>			
				</div>
EOD;
			}
				
			$form .="</form>";
			return $form;
		}

		

		function generateHTML(&$db) {
			$tableWriter = new CmsTableWriter('content',$db);
			ob_start();
			include(CMS_PAGEBUILDER_TEMPLATES . "pageBuilder.php");	
			return ob_get_clean();
		}
		
		
	}
